package com.tsingteng.cosfun.ui.costar.drafts;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.bean.JoinCostarBean;
import com.tsingteng.cosfun.callback.OnDBSaveListener;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.costar.CostarActivity;
import com.tsingteng.cosfun.utils.DBUtils;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.dialog.ContentDialog;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 : 视屏草稿箱
 * 创建人: 肖利.
 * 时间:2018/8/10
 * qq(872179966)
 */

public class VideoDraftsFragment extends BaseFragment {
    @BindView(R.id.rv_rv)
    MyRecycleView rvRv;
    private DraftAdapter mAdapter;
    private  ContentDialog contentDialog;
    private List<JoinCostarBean> allJoinCostarBean;

    @Override
    protected void initView() {
        contentDialog = new ContentDialog(getContext());
        mAdapter = new DraftAdapter();
        rvRv.setLayoutManager(new GridLayoutManager(getContext(),3, LinearLayoutManager.VERTICAL,false));
        rvRv.setAdapter(mAdapter);
        rvRv.isCanRefresh(false);
        allJoinCostarBean = DBUtils.getAllJoinCostarBean();
        if (allJoinCostarBean==null) {
            getActivity().finish();
        }
        Collections.reverse(allJoinCostarBean);
        mAdapter.addData(allJoinCostarBean);
        ((DraftsAcitvity)getActivity()).setVideoNumber(allJoinCostarBean.size()+"");
    }

    public void refreshData() {
        allJoinCostarBean = DBUtils.getAllJoinCostarBean();
        mAdapter.getData().clear();
        Collections.reverse(allJoinCostarBean);
        mAdapter.addData(allJoinCostarBean);
        mAdapter.notifyDataSetChanged();
        ((DraftsAcitvity)getActivity()).setVideoNumber(allJoinCostarBean.size()+"");
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_drafts;
    }

    public class DraftAdapter extends BaseQuickAdapter<JoinCostarBean,BaseViewHolder> {



        public DraftAdapter() {
            super(R.layout.item_draft);

        }

        @Override
        protected void convert(BaseViewHolder helper, JoinCostarBean item) {
            ImageView view = helper.getView(R.id.iv_video_cover);
            ImageView mIvDetele = helper.getView(R.id.iv_delete_video);
            view.measure(0,0);
            String videoCover = "";
            if (!TextUtils.isEmpty(item.getVideoCverUrl())) {
                 videoCover = item.getVideoCverUrl();

            }else {
                videoCover = item.getVideoSectionInfos().get(0).getLocalUrl();
            }
            ImageUtils.LoadImage(mContext,videoCover,view);
            mIvDetele.setOnClickListener(v -> {
                if (contentDialog ==null) new ContentDialog(mContext);
                contentDialog.setContent("确定删除草稿?").setAffirmContent("删除").setAffirmContentColor(R.color.red_bag_color).setCancelContent("取消").setOnTwoClickListener(new ContentDialog.OnTwoClickListener() {
                    @Override
                    public void onAffirm() {
                        DBUtils.deleteJoinBean(item.getVideoType(), new OnDBSaveListener() {
                            @Override
                            public void onSaveSuccess() {
                                refreshData();
                                showToast("删除成功!");
                            }

                            @Override
                            public void onSaveFail() {
                                showToast("删除失败!");
                            }
                        });
                    }

                    @Override
                    public void onCancel() {

                    }
                }).show();
            });

            view.setOnClickListener(v -> {
                VideoHelper.getIntence().setDirection("direct");
                VideoHelper.getIntence().startShootFormDraft(item);
                VideoHelper.getIntence().setmJoinCostarBean(item);
                if ("1".equals(item.getIsActivity())) {
                    VideoHelper.getIntence().setActivityId(item.getActivityId());
                } else {
                    VideoHelper.getIntence().setActivityId(-1);
                }
                startActivity(new Intent(getContext(), CostarActivity.class));
                getActivity().overridePendingTransition(R.anim.activity_up, R.anim.activity_down_alpha);


            });
        }
    }

}
